package com.yanqu.road.server.cmd.cross3;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.rank.eRankInfo;
import com.yanqu.road.entity.enums.rank.eRankRangeType;
import com.yanqu.road.pb.cross3.activity.ChannelRankActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.cross3.ChannelRankActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.List;

@Cmd(code = GameProtocol.S_CHANNEL_RANK_GET_RANK_TOP, desc = "获取跨地区冲榜榜首")
public class ChannelRankActivityGetRankTopCmd extends AbstractCommand {

    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ChannelRankActivityProto.GetChannelRankTopReqMsg reqMsg = ChannelRankActivityProto.GetChannelRankTopReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = ChannelRankActivityMgr.getInfoMap().get(activityId);
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        
        int userConditionType = 0;
        int channelConditionType = 0;
        List<eRankInfo> eRankInfoList = eRankInfo.getERankInfoList(activityInfo.getType(), activityInfo.getChildType());
        for (eRankInfo rankInfo : eRankInfoList) {
            if (rankInfo.getRangeType() == eRankRangeType.User) {
                userConditionType = rankInfo.getConditionType();
                break;
            }
        }
        for (eRankInfo rankInfo : eRankInfoList) {
            if (rankInfo.getRangeType() == eRankRangeType.Channel) {
                channelConditionType = rankInfo.getConditionType();
                break;
            }
        }

        ChannelRankActivityProto.CrossGetChannelRankTopReqMsg.Builder builder = ChannelRankActivityProto.CrossGetChannelRankTopReqMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setUserConditionType(userConditionType);
        builder.setChannelConditionType(channelConditionType);
        player.sendPacket(Cross3Protocol.C3_CHANNEL_RANK_GET_RANK_TOP, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            ChannelRankActivityProto.GetChannelRankTopRespMsg.Builder builder = ChannelRankActivityProto.GetChannelRankTopRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_CHANNEL_RANK_GET_RANK_TOP, builder);
        }
    }
}
